layui.use(['form', "table", "treeSelect"], function () {
    let form = layui.form,
        upload = layui.upload,
        layer = layui.layer,
        $ = layui.$,
        treeSelect = layui.treeSelect,
        table = layui.table;

    //渲染父类别下拉树
    treeSelect.render({
        // 选择器
        elem: '#categoryId',
        // 数据
        data: ctx + '/api/categories/tree',
        // 异步加载方式：get/post，默认get
        type: 'get',
        // 占位符
        placeholder: '请选择商品类别',
        // 点击回调
        click: function (d) {
            //console.log(d);
        },
        // 加载完成后的回调函数
        success: function (d) {
            //console.log(d);
            // 选中节点，根据id筛选
            /*if (parentId) {
                treeSelect.checkNode('parentId', parentId);
            }*/

            //获取zTree对象，可以调用zTree方法
            //var treeObj = treeSelect.zTree('tree');
            //console.log(treeObj);

            //刷新树结构
            //treeSelect.refresh();
        }
    });

    //富文本编辑器初始化
    const {createEditor, createToolbar} = window.wangEditor
    //创建一个配置对象
    const editorConfig = {
        placeholder: '请输入商品详情',
        onChange(editor) {
            //内容改变触发事件
            //const html = editor.getHtml()
        },
        /*配置图片上传*/
        MENU_CONF: {
            uploadImage: {
                server: ctx + '/api/upload/images?type=good',
                fieldName: "file"
            }
        }
    };

    //创建富文本编辑器对象
    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default',
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default',
    });
    //富文本编辑器配置结束

    //上传图片
    let uploadInst = upload.render({
        elem: '#pic-upload-btn',
        url: ctx + '/api/upload/images?type=good',
        before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#pic_preview').attr('src', result); // 图片链接（base64）
            });
        },
        done: function (resp) {
            //上传成功之后的操作
            if (resp.success) {
                $("#pic_preview").attr("data-url", resp.url);
                console.log(resp);
            }
        },
        error: function (resp) {
            console.log(resp);
        }
    });

    //监听提交
    form.on('submit(save-btn)', function (data) {
        //取出已上传成功的图片地址
        data.field.picUrl = $("#pic_preview").attr("data-url");
        //取出富文本编辑器的内容
        data.field.detail = editor.getHtml();

        let url = ctx + "/api/goods";
        $.ajax({
            url,
            method: "post",
            dataType: "json",
            data: data.field,
            success(resp) {
                if (resp.success) {//成功添加记录
                    //重新加载表格
                    parent.layui.use(["table"], function () {
                        let table = parent.layui.table;
                        table.reload('tbl');
                    });

                    let iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                }
            },
            error(resp) {
                console.log(resp);
            }
        });
        return false;
    });

});